﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using DevComponents.DotNetBar.Controls;
using DevComponents.DotNetBar;
using ESRI.ArcGIS.Carto;
using DevComponents.AdvTree;
using System.IO;
using System.Drawing;
using ESRI.ArcGIS.Controls;
using System.Runtime.InteropServices;
using PlanningGIS.ArcGIS.Custom;
using System.Windows.Forms;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Geodatabase;
using PlanningGIS.ArcGIS;

namespace PlanningGIS.Engine.Controls
{
    /// <summary>
    /// 图层树下拉列表控件。
    /// 使用时通过OnCreate创建控件。
    /// </summary>
    public partial class LayersComboBox : ComboTree
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public LayersComboBox()
        {
            _Layers = new LayersBase()
            {
                CheckBoxVisiable = false,
                HasRoot = false
            };
            InitializeComponent();
            if (this.DropDownWidth < 300)
                this.DropDownWidth = 300;
            if (this.DropDownHeight < 320)
                this.DropDownHeight = 320;
            _Layers.Nodes = this.Nodes;            
            SetStyle(System.Windows.Forms.ControlStyles.AllPaintingInWmPaint | System.Windows.Forms.ControlStyles.OptimizedDoubleBuffer |
                 System.Windows.Forms.ControlStyles.ResizeRedraw | System.Windows.Forms.ControlStyles.UserPaint, true);
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="container"></param>
        public LayersComboBox(IContainer container)
            : this()
        {
            container.Add(this);
        }

        #region 属性、变量
        
        /// <summary>
        /// 图层基本对象
        /// </summary>
        private LayersBase _Layers = null;

        /// <summary>
        /// 图层组是否可选
        /// </summary>
        private bool _CompositeLayerEnabled = false;

        /// <summary>
        /// 获取或设置需要控制的图层类型，多种可以用 | 连接，如 zjgisFeatureVector | zjgisFeatureRaster
        /// </summary>
        [Description("设置需要控制的图层数据类型")]
        [DefaultValue(zjgisFeatureType.All)]
        public zjgisFeatureType FeatureType
        {
            get { return _Layers.FeatureType; }
            set { _Layers.FeatureType = value; }
        }

        /// <summary>
        /// 获取或设置需要控制的图层空间数据类型，多种可以用 | 连接，如 zjgisShapePoint | zjgisShapeLine
        /// </summary>
        [Description("设置需要控制的图层空间数据类型")]
        [DefaultValue(zjgisShapeType.All)]
        public zjgisShapeType ShapeType
        {
            get { return _Layers.ShapeType; }
            set { _Layers.ShapeType = value; }
        }

        /// <summary>
        /// 获取或设置扩展的节点
        /// </summary>
        [Description("获取或设置扩展的节点")]
        [Browsable(false)]
        public string[] NodesExpand
        {
            get { return _Layers.NodesExpand; }
            set { _Layers.NodesExpand = value; }
        }

        /// <summary>
        /// 获取选择的图层对象
        /// </summary>
        [Description("获取选择的图层对象")]
        [Browsable(false)]
        public ILayer SelectedLayer
        {
            get
            {
                if (this.Nodes == null || this.Nodes.Count == 0) return null;
                if (this.SelectedNode == null || this.SelectedNode.Tag == null) return null;
                if (this.SelectedNode.Tag == null) return null;
                if (this.SelectedNode.Tag is ILayer) return this.SelectedNode.Tag as ILayer;
                return null;
            }
            set
            {
                SetSelectLayer(value, this.Nodes);
            }
        }

        /// <summary>
        /// 获取选择的图层对应的数据名称
        /// </summary>
        [Description("获取选择的图层对应的数据名称")]
        [Browsable(false)]
        public string SelectDataName
        {
            get
            {
                if (SelectedLayer == null) return "";
                if (SelectedLayer is IFeatureLayer)
                {
                    return ((SelectedLayer as IFeatureLayer).FeatureClass as IDataset).Name;
                }
                return SelectedLayer.Name;
            }
        }
        /// <summary>
        /// 获取或设置是否只显示可见图层
        /// </summary>
        [Description("获取或设置是否只显示可见图层")]
        [DefaultValue(false)]
        public bool IsOnlyVisiable
        {
            get { return _Layers.IsOnlyVisiable; }
            set { _Layers.IsOnlyVisiable = value; }
        }

        /// <summary>
        /// 获取或设置图层组是否可选
        /// </summary>
        [Description("获取或设置图层组是否可选")]
        public bool CompositeLayerEnabled
        {
            get { return _Layers.CompositeLayerEnabled; }
            set
            {
                _CompositeLayerEnabled = value;
                _Layers.CompositeLayerEnabled = _CompositeLayerEnabled;
            }
        }

        /// <summary>
        /// 获取或设置是否只显示可选图层
        /// </summary>
        [Description("获取或设置是否只显示可选图层")]
        [DefaultValue(false)]
        public bool IsOnlySelected
        {
            get { return _Layers.IsOnlySelected; }
            set { _Layers.IsOnlySelected = value; }
        }

        #endregion

        /// <summary>
        /// 绑定地图对象
        /// </summary>
        /// <param name="hook">需要绑定的对象：AxMapControl.Object</param>
        /// <param name="tocObject">toc控件的object</param>
        public void OnCreate(object hook, object tocObject = null)
        {
            _Layers.CompositeLayerEnabled = _CompositeLayerEnabled;
            _Layers.OnCreate(hook, tocObject);
        }

        /// <summary>
        /// 隐藏指定的图层，其它的图层显示
        /// </summary>
        /// <param name="layer">要隐藏的图层</param>
        public void HideLayer(ILayer layer)
        {
            _Layers.HideLayer(layer, _Layers.Nodes);
        }

        /// <summary>
        /// 设置选中的要素类名称
        /// </summary>
        /// <param name="name"></param>
        public void SetFeatureClassName(string name)
        {
            if (string.IsNullOrWhiteSpace(name)) return;
            _Layers.SelectedNode = null;
            _Layers.SetFeatureClassName(name, _Layers.Nodes);
            this.SelectedNode = _Layers.SelectedNode;
        }

        /// <summary>
        /// 获取所有可见的图层
        /// </summary>
        /// <returns></returns>
        public List<ILayer> GetVisiables()
        {
            //MapHelper.GetVisibleLayers(null);
            return null;
        }

        /// <summary>
        /// 获取所有可选的图层
        /// </summary>
        /// <returns></returns>
        public List<ILayer> GetSelectables()
        {
            return null;
        }

        /// <summary>
        /// 获取所有的图层
        /// </summary>
        /// <returns></returns>
        public List<ILayer> GetAllLayers()
        {
            return null;
        }

        /// <summary>
        /// 设置已选择的图层
        /// </summary>
        /// <param name="layer"></param>
        /// <param name="nodes"></param>
        private void SetSelectLayer(ILayer layer, NodeCollection nodes)
        {
            foreach (Node node in nodes)
            {
                if (node.Tag != null && node.Tag is ILayer)
                {
                    
                }
                if (node.Nodes.Count > 0)
                {
                    SetSelectLayer(layer, node.Nodes);
                }
            }
        }

        /// <summary>
        /// 将图层名称列到组合框中
        /// </summary>
        /// <param name="cbo"></param>
        /// <param name="pMap"></param>
        /// <param name="geometryType"></param>
        /// <param name="featureType"></param>
        /// <param name="needVisable"></param>
        /// <param name="clearOldItem"></param>
        public static void FillFeatLyrName2cbo(ComboBox cbo, IMap pMap, esriGeometryType geometryType, esriFeatureType featureType, Boolean needVisable, bool clearOldItem)
        {
            if (clearOldItem) cbo.Items.Clear();

            List<IFeatureLayer> pFeatLyrs;
            pFeatLyrs =MapHelper.GetFeaturelayers(pMap, geometryType, featureType, needVisable);

            if (pFeatLyrs == null) return;
            if (pFeatLyrs.Count == 0) return;

            IFeatureLayer pFeatLyr;
            for (int i = 0; i < pFeatLyrs.Count; i++)
            {
                pFeatLyr = pFeatLyrs[i];
                cbo.Items.Add(pFeatLyr.Name);

            }
        }
    }
}
